package com.virgo.demo.system.controller.admin;

import com.virgo.common.entity.IResponse;
import com.virgo.common.entity.ListDataVo;
import com.virgo.common.utils.IValidateUtils;
import com.virgo.demo.core.annotation.Log;
import com.virgo.demo.core.consts.SiteConst;
import com.virgo.demo.system.domain.bo.AdminUserBo;
import com.virgo.demo.system.domain.vo.AdminUserVo;
import com.virgo.demo.system.service.AdminUserService;
import com.virgo.limit.annotation.RepeatSubmit;
import com.virgo.web.utils.IRequestUtils;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.util.ObjectUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/manage168/adminUser")
@RequiredArgsConstructor
public class AdminUserController {

	private final AdminUserService adminUserService;

	@RequestMapping(value = "/save", method = RequestMethod.POST)
	@Log(module = "管理员", type = "编辑", permission = "admin_user_save")
	@RepeatSubmit
	public IResponse<Void> saveAction(@Validated AdminUserBo adminUserBo) {
		if (!SiteConst.yesOrNoList.contains(adminUserBo.getStatus())) {
			return IResponse.fail("状态参数错误");
		}
		adminUserBo.setAdminName(adminUserBo.getAdminName().toLowerCase());
		adminUserService.saveAdminUser(adminUserBo);
		return IResponse.ok();
	}

	@RequestMapping(value = "/del", method = RequestMethod.POST)
	@Log(module = "管理员", type = "删除", permission = "admin_user_del")
	@RepeatSubmit
	public IResponse<Void> delAction(HttpServletRequest request) {
		String ids = IRequestUtils.getString(request,"ids","");
		if (ObjectUtils.isEmpty(ids)) {
			return IResponse.fail("参数错误");
		}
		adminUserService.deleteUser(ids);
		return IResponse.ok();
	}

	@RequestMapping(value = "/status", method = RequestMethod.POST)
	@Log(module = "管理员", type = "编辑", permission = "admin_user_status")
	@RepeatSubmit
	public IResponse<Void> statusAction(HttpServletRequest request) {
		Long id = IRequestUtils.getLong(request,"id");
		Integer status = IRequestUtils.getInteger(request,"status");
		if (ObjectUtils.isEmpty(id) || id.compareTo(0L) < 1 || !SiteConst.yesOrNoList.contains(status)) {
			return IResponse.fail("参数错误");
		}
		adminUserService.updateStatus(id,status);
		return IResponse.ok();
	}

	@RequestMapping(value = "/reset", method = RequestMethod.POST)
	@Log(module = "管理员", type = "编辑", permission = "admin_user_reset")
	@RepeatSubmit
	public IResponse<Void> resetAction(HttpServletRequest request) {
		Long id = IRequestUtils.getLong(request,"id");
		String password = IRequestUtils.getString(request,"password","");
		if (ObjectUtils.isEmpty(id) || id.compareTo(0L) < 1 || !IValidateUtils.password(password)) {
			return IResponse.fail("参数错误");
		}
		adminUserService.resetPassword(id,password);
		return IResponse.ok();
	}

	@RequestMapping(value = "/list", method = RequestMethod.POST)
	@Log(permission = "admin_user_list", saveLog = false)
	public IResponse<ListDataVo<AdminUserVo>> listAction(HttpServletRequest request) {
		return IResponse.ok(adminUserService.findList(request));
	}
}